from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from services import formula_service
from db.database import get_db
from db import models
from pydantic import BaseModel

# Pydantic model for response
class FormulaResponse(BaseModel):
    id: int
    latex: str

    class Config:
        from_attributes = True

router = APIRouter()

@router.get("/random", response_model=FormulaResponse)
def get_random_formula(db: Session = Depends(get_db)):
    """Fetches a random formula from the database."""
    formula = formula_service.get_random_formula(db)
    if formula is None:
        raise HTTPException(status_code=404, detail="No formulas found in the database.")
    return formula
